﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Package;
using System.IO;
using System.Globalization;
using Microsoft.VisualStudio;
using VsCommands = Microsoft.VisualStudio.VSConstants.VSStd97CmdID;
using VsCommands2K = Microsoft.VisualStudio.VSConstants.VSStd2KCmdID;
namespace dragonangel.phpProject
{
    public class phpProjectNode : ProjectNode
    {
        public phpProjectNode(phpProjectPackage package)
        {
            Package = package;
            this.SupportsProjectDesigner = true;
            // Add Category IDs mapping in order to support properties for project items
            AddCATIDMapping(typeof(FileNodeProperties), typeof(FileNodeProperties).GUID);
            AddCATIDMapping(typeof(ProjectNodeProperties), typeof(ProjectNodeProperties).GUID);
            AddCATIDMapping(typeof(FolderNodeProperties), typeof(FolderNodeProperties).GUID);
        }

        public override Guid ProjectGuid
        {
            get { return GuidList.guidphpProjectFactory; }
        }

        public override string ProjectType
        {
            get { return this.GetType().Name; }
        }

        public override void AddChild(HierarchyNode node)
        {
            base.AddChild(node);
        }

        protected override bool CanDeleteItem(Microsoft.VisualStudio.Shell.Interop.__VSDELETEITEMOPERATION deleteOperation)
        {
            return base.CanDeleteItem(deleteOperation);
        }

        /// <summary>
        /// Make sure that we load normally and skip security checking. 
        /// This can have security impacts so do not do this in production code.
        /// </summary>
        protected override ProjectLoadOption CheckProjectForSecurity(ProjectSecurityChecker projectSecurityChecker, ProjectSecurityChecker userProjectSecurityChecker)
        {
            return ProjectLoadOption.LoadNormally;
        }

        /// <summary>
        /// Overriding to provide customization of files on add files.
        /// This will replace tokens in the file with actual value (namespace, class name,...)
        /// </summary>
        /// <param name="source">Full path to template file.</param>
        /// <param name="target">Full path to destination file.</param>
        /// <exception cref="FileNotFoundException">Template file is not fount.</exception>
        public override void AddFileFromTemplate(string source, string target)
        {
            if (!File.Exists(source))
            {
                throw new FileNotFoundException(string.Format("Template file not found: {0}", source));
            }

            // The class name is based on the new file name
            string fileName = Path.GetFileNameWithoutExtension(target);

            this.FileTemplateProcessor.Reset();
            this.FileTemplateProcessor.AddReplace("%className%", fileName);

            try
            {
                this.FileTemplateProcessor.UntokenFile(source, target);
            }
            catch (Exception exceptionObj)
            {
                throw new FileLoadException(Resources.ResourceManager.GetString("MsgFailedToLoadTemplateFile"), target, exceptionObj);
            }
        }

        protected override ReferenceContainerNode CreateReferenceContainerNode()
        {
            return null;
        }

        protected override int QueryStatusOnNode(Guid cmdGroup, uint cmd, IntPtr pCmdText, ref QueryStatusResult result)
        {
            if (cmdGroup == VsMenus.guidStandardCommandSet2K)
            {
                if (VsCommands2K.ADDREFERENCE == ((VsCommands2K)cmd))
                {
                    result |= QueryStatusResult.NOTSUPPORTED | QueryStatusResult.INVISIBLE;
                    return VSConstants.S_OK;
                }
            }
            return base.QueryStatusOnNode(cmdGroup, cmd, pCmdText, ref result);
        }

        protected override QueryStatusResult QueryStatusCommandFromOleCommandTarget(Guid guidCmdGroup, uint cmd, out bool handled)
        {
            if (guidCmdGroup == VsMenus.guidStandardCommandSet2K)
            {
                switch ((VsCommands2K)cmd)
                {
                    case VsCommands2K.ADDREFERENCE:
                        handled = false;
                        return QueryStatusResult.NOTSUPPORTED | QueryStatusResult.INVISIBLE;
                }
            }
            return base.QueryStatusCommandFromOleCommandTarget(guidCmdGroup, cmd, out handled);

        }

        public override int QueryDeleteItem(uint delItemOp, uint itemId, out int candelete)
        {
            Microsoft.VisualStudio.Shell.Interop.__VSDELETEITEMOPERATION delItem = (Microsoft.VisualStudio.Shell.Interop.__VSDELETEITEMOPERATION)delItemOp;
            int result=base.QueryDeleteItem(delItemOp, itemId, out candelete);
            if (delItem == Microsoft.VisualStudio.Shell.Interop.__VSDELETEITEMOPERATION.DELITEMOP_RemoveFromProject)
                candelete = 1;
            return result;
        }


    }
}
